;
(function (window, angular) {
	'use strict';
	angular.module("Controller.System.DictionarySearch", [])
		.controller("DictionarySearchCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$timeout", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $timeout) {
			$scope.activeFlag = [true];
			$scope.markInfo = {
				allChecked: false
			};
			$scope.pageInfo = {
				totalPages: 1,
				totalRows: 0,
				pageSize: 10,
				curPage: 1
			};
			$scope.condition = {};
			$scope.toolbarConfig = {
				showAdvanceSearch: false
			};
			$scope.dataList = [];
			$scope.dataSelected = {
				role: {},
				department: {}
			};

			$scope.changeTab = function (index) {
				$scope.activeFlag = [false];
				$scope.activeFlag[index] = true;
				$scope.submitted = false;
				switch (index) {
					case 0:
						initData();
						break;
					default:
						break;
				}
			};
			function initData() {
				$scope.dataSelected = {
					role: {},
					department: {}
				};
			}

			function getDataList(curPage) {
				var service = "device";
				var reqUrl = "/dataDictionary/listAll";
				var param = {};
				param.pageSize = $scope.pageInfo.pageSize;
				if (!!curPage) {
					$scope.pageInfo.curPage = curPage;
				}
				param.pageNum = $scope.pageInfo.curPage;
				param.filters = $scope.condition;
				ybHttp.Post(service, reqUrl, param).then(function (resp) {
					if (resp.code !== 0) {
						ybWidget.ShowMessage(resp, MESG_TYPE.Error);
						return;
					}
					angular.forEach(resp.value.list, function (item) {
						item.isChecked = false;
					});
					$scope.pageInfo.totalRows = resp.value.total;
					$scope.pageInfo.totalPages = resp.value.pages;
					$scope.dataList = resp.value.list;
				});
			}

			$scope.switchToolbar = function (toolbar) {
				if (!!$scope.toolbarConfig["show" + toolbar]) {
					$scope.toolbarConfig["show" + toolbar] = false;
				} else {
					$timeout(function () {
						$scope.toolbarConfig["show" + toolbar] = true;
					});
				}
			};

			$scope.selectRow = function (index) {
				$scope.markInfo.cueRow = index
			};

			$scope.switchAdvance = function () {
				$scope.showAdvance = !$scope.showAdvance;
			};

			$scope.search = function (curpage) {
				getDataList(curpage);
			};

			$scope.reset = function () {
				$scope.condition = {};
			};

			$scope.selectAll = function () {
				for (var i = 0, len = $scope.dataList.length; i < len; i++) {
					$scope.dataList[i].isChecked = $scope.markInfo.allChecked;
				}
			};

			function checkAll() {
				var selectAll = false;
				for (var i = 0, len = $scope.dataList.length; i < len; i++) {
					if (!$scope.dataList[i].isChecked) {
						selectAll = false;
						break;
					}
					selectAll = true;
				}
				return selectAll;
			}

			$scope.selectSingle = function (item) {
				if (!item.isChecked) {
					$scope.markInfo.allChecked = false;
				} else {
					if (!checkAll()) {
						$scope.markInfo.allChecked = false;
					} else {
						$scope.markInfo.allChecked = true;
					}
				}
			};

			(function () {
				getDataList();
			})();
		}])
	;
})(window, window.angular);